<?php
namespace app\admin\controller;

use app\common\model\Banner as BannerModel;
use app\common\model\OneClass as OneClassModel;
use app\common\model\Cate as CateModel;
use app\common\model\MusicCate as MusicCateModel;
use app\common\model\Music as MusicModel;
use app\common\model\Company as CompanyModel;
use app\common\model\VideoCate as VideoCateModel;
use app\common\model\Video as VideoModel;
use app\common\model\News as NewsModel;
use app\common\model\NewsComment as NewsCommentModel;

/**
 * 资讯控制器
 * Class Info
 * @package app\index\controller
 */
class Info extends Base
{
    /**
     * [getIndex  查看]
     * @param $data ['type'] 1banner 2查询单条banner 3资讯分类 4所有资讯
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $data = input('param.');
        $this->getPageAndSize($data);
        $model = new OneClassModel();
        $catemodel = new CateModel();
        $musiccatemodel = new MusicCateModel();
        $videocatemodel = new VideoCateModel();
        $map = [];
        $user = $this->user;
        if (!empty($data['type'])) {
            if($data['type'] == 1){
                $model = new BannerModel();
                try{
                    $map['status'] = ['in','1,2'];
                    $count = $model->getCount($map);//满足条件的总数
                    $allpage = intval(ceil($count / $this->size));//总页数
                    $lists = $model->getlist($map,$this->from, $this->size);
                }catch(\Exception $e){
                    return admin_json(-1, '获取失败'.$e->getMessage());
                }
                foreach($lists as $k=>$v){
                    $http = substr($v['pic_url'],0,4);
                    if($http != 'http'){
                        $lists[$k]['pic_url'] = config('domain_name').$v['pic_url'];
                    }
                    $lists[$k]['addtime'] = date('Y-m-d',$v['addtime']);
                }
                writelog('查看Banner列表',1,$user);
                return admin_json(1,'获取成功',[
                    'count' => $count,//总记录数
                    'allpage' => $allpage,  //总页数
                    'lists' => $lists,  //列表数据
                ]);
            }elseif($data['type'] == 2){
                $model = new BannerModel();
                try{
                    $list = $model->findData($data['id']);
                    $http = substr($list['pic_url'],0,4);
                    if($http != 'http'){
                        $list['pic_url'] = config('domain_name').$list['pic_url'];
                    }
                }catch(\Exception $e){
                    return admin_json(-1, '获取失败'.$e->getMessage());
                }
                writelog('查看Banner'.$data['id'],1,$user);
                return admin_json(1, '获取成功',$list);
            }elseif($data['type'] == 3){
                try{
                    $list = $model->getAllData();
                    $map['states'] = ['neq' ,'-1'];
                    foreach($list as $k=>$v){
                        if($v['info'] == 'cate'){
                            $map['class_id'] = $v['id'];
                            $cate = $catemodel->getAllData($map);
                            $list[$k]['info'] = $cate;
                        }elseif($v['info'] == 'musiccate'){
                            $map['class_id'] = $v['id'];
                            $musiccate = $musiccatemodel->getAllData($map);
                            $list[$k]['info'] = $musiccate;
                        }elseif($v['info'] == 'videocate'){
                            $map['class_id'] = $v['id'];
                            $videocate = $videocatemodel->getAllData($map);
                            $list[$k]['info'] = $videocate;
                        }
                    }
                }catch(\Exception $e){
                    return admin_json(-1, '获取失败'.$e->getMessage());
                }
                writelog('查看资讯分类',1,$user);
                return admin_json(1, '获取成功',$list);
            }elseif($data['type'] == 4){
                if (is_array($data)) {
                    if (!empty($data['con']))
                        $map['n.title'] = ['like', "%" . $data['con'] . "%"];

                    if(!empty($data['start']) && !empty($data['end'])){
                        $map['n.addtime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
                    }
                    if (isset($data['recommend'])){
                        $map['n.recommend'] = $data['recommend'];
                    }
                    if (isset($data['recommend1'])){
                        $map['n.recommend1'] = $data['recommend1'];
                    }
                    if (!empty($data['istop'])){
                        $map['n.istop'] = $data['istop'];
                    }
                }

                $cmap['states'] = ['neq',-1];
                if($data['lm_type'] == 1){
                    $name = '资讯';
                    $cmap['class_id'] = 1;
                    $cate = $catemodel->getAllData($cmap);
                }elseif ($data['lm_type'] == 2){
                    $name = '佛经';
                    $cmap['class_id'] = 2;
                    $cate = $catemodel->getAllData($cmap);
                }elseif ($data['lm_type'] == 3){
                    $name = '佛片';
                    $cmap['class_id'] = 3;
                    $cate = $videocatemodel->getAllData($cmap);
                }elseif ($data['lm_type'] == 4){
                    $name = '佛乐';
                    $cmap['class_id'] = 4;
                    $cate = $musiccatemodel->getAllData($cmap);
                }elseif ($data['lm_type'] == 5){
                    $name = '禅语';
                    $cmap['class_id'] = 5;
                    $cate = $musiccatemodel->getAllData($cmap);
                }

                $ids = '';
                foreach($cate as $k=>$v){
                    if(empty($ids)){
                        $ids = $v['id'];
                    }else{
                        $ids .= ','.$v['id'];
                    }
                }

                try {
                    $map['n.states'] = ['not in' ,'2,-1'];
                    if($data['lm_type'] == 1 || $data['lm_type'] == 2){
                        $map['n.cateid'] = ['in',$ids];
                        if(!empty($data['cateid'])){
                            $map['n.cateid'] = $data['cateid'];
                        }
                        if($data['lm_type'] == 2){
                            $field = 'c.cname,n.id,n.title,n.cover,n.comid,n.addtime,n.cateid,n.read_num,n.states,n.intro,n.recommend,n.deltime,n.recommend1,n.istop,n.from,n.jread_num';
                        }else{
                            $field = 'c.cname,n.id,n.title,n.cover,n.comid,n.addtime,n.cateid,n.content,n.read_num,n.states,n.intro,n.recommend,n.deltime,n.recommend1,n.istop,n.from,n.jread_num';
                        }
                        if(!empty($data['read_num'])){
                            $order = ['n.read_num' => $data['read_num']];
                        }else{
                            $order = ['n.id' => 'desc'];
                        }
                        $newmodel = new NewsModel;
                        $cmodel = new CompanyModel;
                        $count = $newmodel->getCount($map);//满足条件的总数
                        $allpage = intval(ceil($count / $this->size));//总页数
                        $lists = $newmodel->getlist($map, $this->from, $this->size,$field,$order);
                    }elseif ($data['lm_type'] == 3){
                        $map['n.itemid'] = ['in',$ids];
                        if(!empty($data['cateid'])){
                            $map['n.itemid'] = $data['cateid'];
                        }
                        $videomodel = new VideoModel();
                        $count = $videomodel->getCount($map);//满足条件的总数
                        $allpage = intval(ceil($count / $this->size));//总页数
                        $lists = $videomodel->getlist($map, $this->from, $this->size);
                    }else{
                        $map['n.itemid'] = ['in',$ids];
                        if(!empty($data['cateid'])){
                            $map['n.itemid'] = $data['cateid'];
                        }
                        $musicmodel = new MusicModel();
                        $count = $musicmodel->getCount($map);//满足条件的总数
                        $allpage = intval(ceil($count / $this->size));//总页数
                        $lists = $musicmodel->getlist($map, $this->from, $this->size);
                    }
                    foreach($lists as $k=>$v){
                        $lists[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                        if(!empty($v['content'])){
                            $content = percent1($v['content']);
                            $lists[$k]['content'] = get_img_thumb_url($content,config('domain_name').'/');
                        }
                        if(!empty($v['comid'])){
                            $company = $cmodel->findData($v['comid'],'id','comname');
                            $lists[$k]['comname'] = $company['comname'];
                        }

                        if($data['lm_type'] == 1 || $data['lm_type'] == 2){
                            $ncmodel = new NewsCommentModel;
                            $v['pinlun_num'] = $ncmodel->getAllCount(['nid'=>$v['id']]);
                            $http = substr($v['cover'],0,4);
                            if($http != 'http'){
                                $lists[$k]['cover'] = config('domain_name').$v['cover'];
                            }
                        }else{
                            $imghttp = substr($v['img'],0,4);
                            if($imghttp != 'http'){
                                $lists[$k]['img'] = config('domain_name').$v['img'];
                            }
                            $urlhttp = substr($v['url'],0,4);
                            if($urlhttp != 'http'){
                                $lists[$k]['url'] = config('domain_name').$v['url'];
                            }
                        }
                    }
                }catch(\Exception $e){
                    writelog($e->getMessage(), -1);
                    return admin_json(-1, '获取失败'.$e->getMessage());
                }

                writelog('查看'.$name.'列表',1,$user);
                return admin_json(1,'获取成功',[
                    'count' => $count,//总记录数
                    'allpage' => $allpage,  //总页数
                    'lists' => $lists,  //列表数据
                ]);

            }
        }
    }
    /**
     * [postAdd  添加]
     * @param $data['ftype'] 1添加banner 2创建分类 3添加所有资讯
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postAdd()
    {
        $data = input('param.');
        $user = $this->user;
        if(!empty($data['ftype'])){
            if($data['ftype'] == 1){
                $model = new BannerModel();
                try{
                    $data['addtime'] = time();
                    $id = $model->addData($data);
                }catch(\Exception $e){
                    return admin_json(-1, '添加失败'.$e->getMessage());
                }
                writelog('添加banner'.$id,1,$user);
                return admin_json(1,'添加成功');
            }elseif ($data['ftype'] == 2){
                try{
                    if($data['type'] == 1){
                        $model =  new CateModel();
                        $data['addtime'] = time();
                        $id = $model->addData($data);
                    }elseif($data['type'] == 2){
                        $model =  new MusicCateModel();
                        $data['addtime'] = time();
                        $id = $model->addData($data);
                    }elseif($data['type'] == 3){
                        $model =  new VideoCateModel();
                        $data['addtime'] = time();
                        $id = $model->addData($data);
                    }
                }catch(\Exception $e){
                    return admin_json(-1, '添加失败'.$e->getMessage());
                }
                writelog('创建资讯分类type：'.$data['type'].',id:'.$id,1,$user);
                return admin_json(1,'添加成功');
            }elseif ($data['ftype'] == 3){
                try{
                    if($data['type'] == 1){
//                        $data1 = array(
//                            'title' => $data['title'],
//                            'cover' => $data['cover'],
//                            'cateid' => $data['cateid'],
//                            'content' => $data['content'],
//                            'intro' => $data['intro'],
//                            'addtime' => time(),
//                        );
//                        if(!empty($data['read_num'])){
//                            $data1['read_num'] = $data['read_num'];
//                        }
//                        if(!empty($data['recommend'])){
//                            $data1['recommend'] = $data['recommend'];
//                        }
                        $name = '资讯';
                        $data['addtime'] = time();
                        $model =  new NewsModel();
                        $id = $model->addData($data);
                    }elseif($data['type'] == 2){
                        $model =  new MusicModel();
                        $data1 = array(
                            'title' => $data['title'],
                            'img' => $data['img'],
                            'itemid' => $data['itemid'],
                            'url' => $data['url'],
                            'addtime' => time(),
                        );
                        if(!empty($data['author'])){
                            $data1['author'] = $data['author'];
                        }
                        $id = $model->addData($data1);
                        $name = '佛乐';
                    }elseif($data['type'] == 3){
                        $model =  new VideoModel();
                        $data['addtime'] = time();
                        $id = $model->addData($data);
                        $name = '佛片';
                    }
                }catch(\Exception $e){
                    return admin_json(-1, '添加失败'.$e->getMessage());
                }
                writelog('创建'.$name.'--'.$id);
                return admin_json(1,'添加成功');

            }
        }
    }
    /**
     * [postEdit  修改]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postEdit()
    {
        $data = input('param.');
        $model = new BannerModel();
        $user = $this->user;
        if(!empty($data['ftype'])) {
            if ($data['ftype'] == 1) {
                try {
                    $model->editData($data);
                } catch (\Exception $e) {
                    return admin_json(-1, '修改失败' . $e->getMessage());
                }
                writelog('修改banner' . $data['id'],1,$user);
                return admin_json(1, '修改成功');
            }elseif($data['ftype'] == 2){
                try {
//                    $arr = array('id'=>$data['id'],'cname'=>$data['cname'],'sort'=>$data['sort']);
                    if($data['type'] == 1){
                        $model =  new CateModel();
                        $model->editData($data);
                        $name = '资讯';
                    }elseif($data['type'] == 2){
                        $model =  new MusicCateModel();
                        $model->editData($data);
                        $name = '佛乐';
                    }elseif($data['type'] == 3){
                        $model =  new VideoCateModel();
                        $model->editData($data);
                        $name = '佛片';
                    }
                } catch (\Exception $e) {
                    return admin_json(-1, '修改失败' . $e->getMessage());
                }
                writelog('修改'.$name.'分类--'. $data['id'],1,$user);
                return admin_json(1, '修改成功');
            }elseif ($data['ftype'] == 3){
                try{
                    $name = '资讯';
                    if($data['type'] == 1){
                        $model =  new NewsModel();
                        if(!empty($data['recommend'])){
                            if($data['recommend'] == 1){
                                $map = [];
                                $map['recommend'] = 1;
                                $map['cateid'] = $data['cateid'];
                                $rcount = $model->getAllCount($map);
                                if($rcount >= 3){
                                    return admin_json(-1, '同一分类下只能推荐3篇');
                                }
                                $name = '资讯首页推荐';
                            }
                        }
                        if(!empty($data['recommend1'])){
                            if($data['recommend1'] == 1){
                                $map = [];
                                $map['recommend1'] = 1;
                                $map['cateid'] = $data['cateid'];
                                $rcount = $model->getAllCount($map);
                                if($rcount >= 3){
                                    return admin_json(-1, '同一分类下只能推荐3篇');
                                }
                                $name = '资讯寺院推荐';
                            }
                        }
                        if(!empty($data['istop'])) {
                            if ($data['istop'] == 1) {
                                $tcount = $model->getAllCount(['istop' => 1, 'cateid' => $data['cateid']]);
                                if ($tcount >= 1) {
                                    return admin_json(-1, '同一分类下只能置顶1篇');
                                }
                                $name = '资讯置顶';
                            }
                        }
                        $id = $model->editData($data);

                    }elseif($data['type'] == 2){
                        $model =  new MusicModel();
                        $id = $model->editData($data);
                        $name = '佛乐';
                    }elseif($data['type'] == 3){
                        $model =  new VideoModel();
                        $id = $model->editData($data);
                        $name = '佛片';
                    }
                }catch(\Exception $e){
                    return admin_json(-1, '修改失败'.$e->getMessage());
                }
                writelog('修改'.$name.'--'.$id,1,$user);
                return admin_json(1,'修改成功');
            }
        }
    }
    /**
     * [postAdd 删除]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getDel()
    {
        $data = input('param.');
        $model = new BannerModel();
        $user = $this->user;
        if(!empty($data['ftype'])) {
            if ($data['ftype'] == 1) {
                try {
                    $data['status'] = 3;
                    $model->editData($data);
                } catch (\Exception $e) {
                    return admin_json(-1, '删除失败' . $e->getMessage());
                }
                writelog('删除banner,id:'. $data['id'],1,$user);
                return admin_json(1,'删除成功');
            }elseif ($data['ftype'] == 2){
                try {
                    $data['states'] = -1;
                    $where['id'] = $data['id'];
                    if($data['type'] == 1){
                        $model =  new CateModel();
                        $nmodel = new NewsModel();
                        $news = $nmodel->findData($data['id'],'cateid');
                        if(!empty($news)){
                            return admin_json(2,'抱歉请先删除分类下的内容');
                        }
                        $name = '资讯';
                        $model->deleteFalseData($where,$data);
                    }elseif($data['type'] == 2){
                        $model =  new MusicCateModel();
                        $nmodel = new MusicModel();
                        $news = $nmodel->findData($data['id'],'itemid');
                        if(!empty($news)){
                            return admin_json(2,'抱歉请先删除分类下的内容');
                        }
                        $name = '佛乐';
                        $model->deleteFalseData($where,$data);
                    }elseif($data['type'] == 3){
                        $model =  new VideoCateModel();
                        $nmodel = new VideoModel();
                        $news = $nmodel->findData($data['id'],'itemid');
                        if(!empty($news)){
                            return admin_json(2,'抱歉请先删除分类下的内容');
                        }
                        $name = '佛片';
                        $model->deleteFalseData($where,$data);
                    }
                } catch (\Exception $e) {
                    return admin_json(-1, '删除失败' . $e->getMessage());
                }
                writelog('删除'.$name.'分类内容--'.$data['id'],1,$user);
                return admin_json(1,'删除成功');
            }elseif ($data['ftype'] == 3){
                try{
                    $data['states'] = -1;
                    $where['id'] = $data['id'];
                    if($data['type'] == 1){
                        $data['deltime'] = time();
                        $model =  new NewsModel();
                        $news = $model->findData($data['id'],'id');
                        if($news['istop'] == 1){
                            return admin_json(-1,'请先取消置顶！');
                        }
                        if($news['recommend'] == 1 || $news['recommend1'] == 1){
                            return admin_json(-1,'请先取消推荐！');
                        }
                        $name = '资讯';
                        $id = $model->deleteFalseData($where,$data);
                    }elseif($data['type'] == 2){
                        $model =  new MusicModel();
                        $name = '佛乐';
                        $id = $model->deleteFalseData($where,$data);
                    }elseif($data['type'] == 3){
                        $model =  new VideoModel();
                        $name = '佛片';
                        $id = $model->deleteFalseData($where,$data);
                    }
                }catch(\Exception $e){
                    return admin_json(-1, '删除失败'.$e->getMessage());
                }
                writelog('删除'.$name.'--'.$id,1,$user);
                return admin_json(1,'删除成功');
            }
        }
    }
    /**
     * [getCate 获取二级分类]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getCates()
    {
        $data = input('param.');
        $user = $this->user;
        try {
            $map['class_id'] = $data['type'];
            $map['states'] = ['neq',-1];
            if($data['type'] == 2 || $data['type'] == 1){
                $model =  new CateModel();
                $list = $model->getAllData($map);
                $name = '资讯';
            }elseif($data['type'] == 4 || $data['type'] == 5){
                $model =  new MusicCateModel();
                $list = $model->getAllData($map);
                $name = '佛乐';
            }elseif($data['type'] == 3){
                $model =  new VideoCateModel();
                $list = $model->getAllData($map);
                $name = '佛片';
            }
        } catch (\Exception $e) {
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        writelog('查看'.$name.'二级分类',1,$user);
        return admin_json(1,'获取成功',$list);
    }
    /**
     * [getCate 资讯回收站]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getRecall(){
        $data = input('param.');
        $this->getPageAndSize($data);
        $catemodel = new CateModel();
        $map = [];
        $user = $this->user;
        if (is_array($data)) {
            if (!empty($data['con']))
                $map['n.title'] = ['like', "%" . $data['con'] . "%"];

            if(!empty($data['start']) && !empty($data['end'])){
                $map['n.addtime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
            }
        }

        $cmap['states'] = ['neq',-1];
        $name = '资讯';
        $cmap['class_id'] = 1;
        $cate = $catemodel->getAllData($cmap);

        $ids = '';
        foreach($cate as $k=>$v){
            if(empty($ids)){
                $ids = $v['id'];
            }else{
                $ids .= ','.$v['id'];
            }
        }

        try {
            $map['n.states'] = -1;
            $map['n.cateid'] = ['in',$ids];
            if(!empty($data['cateid'])){
                $map['n.cateid'] = $data['cateid'];
            }
            $map['n.deltime'] = ['LT',time()+3600*24*30];
            $newmodel = new NewsModel();
            $field = 'c.cname,n.id,n.title,n.cover,n.comid,n.addtime,n.cateid,n.content,n.read_num,n.states,n.intro,n.recommend,n.deltime,n.recommend1,n.istop,n.from,n.jread_num';
            $count = $newmodel->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $newmodel->getlist($map, $this->from, $this->size,$field);

            foreach($lists as $k=>$v){
                $lists[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                if(!empty($v['content'])){
                    $content = percent1($v['content']);
                    $lists[$k]['content'] = get_img_thumb_url($content,config('domain_name').'/');
                }
                $http = substr($v['cover'],0,4);
                if($http != 'http'){
                    $lists[$k]['cover'] = config('domain_name').$v['cover'];
                }
            }
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }

        writelog('查看'.$name.'回收站',1,$user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getCate 资讯回收站清空恢复]
     * @param $param['states'] 1回复 2清空
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getEmpty(){
        $param = input('param.');
        $model = new NewsModel;
        $user = $this->user;
        try{
            $model->editData($param);
        }catch(\Exception $e){
            return admin_json(-1, '失败'.$e->getMessage());
        }
        if($param['states'] == 1){
            $name = '恢复';
        }elseif($param['states'] == 2){
            $name = '清空';
        }
        writelog('资讯回收站'.$name.'：id'.$param['id'],1,$user);
        return admin_json(1, '成功');
    }
    /**
     * [getTop 置顶]
     * @param istop 1置顶首页 2置顶寺院  0取消
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getTop(){
        $param = input('param.');
        $model = new NewsModel;
        $user = $this->user;
        try{
            if($param['istop'] == 1 || $param['istop'] == 2){
                $cateid = $model->findData($param['id'],'id','cateid');
                $new = $model->getAllData(['cateid'=>$cateid['cateid'],'istop'=>$param['istop']]);
                if(!empty($new)){
                    return admin_json(-1, '该分类下已存在置顶');
                }
            }
        }catch(\Exception $e){
            return admin_json(-1, '失败'.$e->getMessage());
        }
        writelog('查看置顶资讯'.$param['id'].','.$param['states'],1,$user);
        return admin_json(1, '成功');
    }
}